package com.mlearning.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.mlearning.demo.mapper.GraphMapper;
import com.mlearning.demo.pojo.utils.Triad;
import com.mlearning.demo.pojo.neo4j.Book;
import com.mlearning.demo.pojo.utils.Link;
import com.mlearning.demo.pojo.utils.Group;
import com.mlearning.demo.pojo.utils.Node;
import com.mlearning.demo.service.GraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("graphService")
public class GraphServiceImpl implements GraphService {
    @Autowired
    private GraphMapper graphMapper;

    private final JSONArray categories=JSON.parseArray("[\n" +
            "{\n" +
            "\"name\": \"中心知识\"\n" +
            "},\n" +
            "{\n" +
            "\"name\": \"相关知识\"\n" +
            "}\n" +
            "]");
    private List<Integer> nodeList;
    private List<Integer> linkList;
    @Override
    public JSONObject creatJson(String search) {
        List<Triad> triads=graphMapper.know2Know(search);
        if (triads.size()==0){
            return null;
        }
        nodeList=new ArrayList<>();
        linkList=new ArrayList<>();
        Group nodes=new Group();
        Group links=new Group();
        addJson(triads,nodes,links);
        JSONObject graphJson=new JSONObject();
        graphJson.put("type","force");
        graphJson.put("categories",categories);
        graphJson.put("nodes",nodes.getList());
        graphJson.put("links",links.getList());
        return graphJson;
    }
    @Override
    public JSONObject updateJson(String search){
        Group nodes=new Group();
        Group links=new Group();
        List<Triad> triads=graphMapper.know2Know(search);
        if (triads.size()==0){
            return null;
        }

        addJson(triads,nodes,links);
        JSONObject graphJson=new JSONObject();
        graphJson.put("nodes",nodes.getList());
        graphJson.put("links",links.getList());
//        graphJson.put("books",bookJson(search).getList());
//        System.out.println(graphJson);
        return graphJson;
    }

//    private Group bookJson(String search){
//        Group books=new Group();
//
//        List<Triad> triads=graphMapper.tag2Book(search);
//        if(triads.size()==0){
//            triads=graphMapper.know2Book(search);
//
//        }
//        System.out.println(triads);
//        JSONObject start=JSON.parseObject(triads.get(0).getEndnode());
//        books.getList().add(new Book(start.getString("name"),start.getString("author"),start.getString("img"),start.getString("quote")));
//        return books;
//
//    }
    private void addJson(List<Triad> triads,Group nodes,Group links){
        JSONObject start;
        JSONObject rel;
        JSONObject end;
        int id;
        for (Triad triad:triads
        ) {
            start=JSON.parseObject(triad.getStartnode());
            id =start.getInteger("id");

            if(!nodeList.contains(id))
            {
                nodeList.add(id);
                Node node=new Node(start.getString("name"),0);
                nodes.getList().add(node);
            }
            int source=nodeList.indexOf(id);

            end= JSON.parseObject(triad.getEndnode());
            id= end.getInteger("id");
            if(!nodeList.contains(id))
            {
                nodeList.add(id);
                Node node=new Node(end.getString("name"),1);
                nodes.getList().add(node);
            }
            int target=nodeList.indexOf(id);

            rel=JSON.parseObject(triad.getRelationship());
            id=rel.getInteger("id");
            if(!linkList.contains(id)){
                linkList.add(id);
                Link link=new Link(source,target);
                links.getList().add(link);
            }
        }

    }
}
